package com.artPreter.service.impl;

import com.artPreter.mapper.ArtistContentSectionMapper;
import com.artPreter.pojo.ArtistContentSection;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.artPreter.mapper.TemplateDetailMapper;
import com.artPreter.pojo.ArtistSectionTemp;
import com.artPreter.mapper.ArtistSectionTempMapper;
import com.artPreter.pojo.TemplateDetail;
import com.artPreter.service.IArtistContentSectionService;
import com.artPreter.service.IArtistSectionTempService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 艺术家栏目表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-23
 */
@Service
public class ArtistSectionTempServiceImpl extends ServiceImpl<ArtistSectionTempMapper, ArtistSectionTemp> implements IArtistSectionTempService {

    @Autowired
    private TemplateDetailMapper templateDetailMapper;
    @Autowired
    private IArtistContentSectionService artistContentSectionService;
    @Autowired
    private ArtistContentSectionMapper artistContentSectionMapper;

    @Override
    public void add(ArtistSectionTemp artistSectionTemp) {
        String uuid = UUID.randomUUID().toString();
        if(artistSectionTemp.getTempId()!=null){
            LambdaQueryWrapper<TemplateDetail> templateDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            templateDetailLambdaQueryWrapper.eq(TemplateDetail::getTempId, artistSectionTemp.getTempId());
            List<TemplateDetail> templateDetailList = templateDetailMapper.selectList(templateDetailLambdaQueryWrapper);
            if(templateDetailList.size()>0){
                // 模板地址
                artistSectionTemp.setTempAddr(templateDetailList.get(0).getTempAddr());
                // 内容类型
                artistSectionTemp.setContentType(templateDetailList.get(0).getContentType());
                // 模板名称
                artistSectionTemp.setTempName(templateDetailList.get(0).getName());

                LambdaQueryWrapper<ArtistSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                if(artistSectionTemp.getArtistId()!=null){
                    lambdaQueryWrapper.eq(ArtistSectionTemp::getArtistId, artistSectionTemp.getArtistId());
                }
                List<ArtistSectionTemp> list = this.baseMapper.selectList(lambdaQueryWrapper);
                if(list.size()==0){
                    artistSectionTemp.setIsDefault(1);
                }
            }
        }
        artistSectionTemp.setSectionId(uuid);
        this.baseMapper.insert(artistSectionTemp);
        // 添加内容板块
        artistContentSectionService.addContent(artistSectionTemp);
    }

    @Override
    public void delete(ArtistSectionTemp artistSectionTemp) {
        this.baseMapper.deleteById(artistSectionTemp);
        //如果该栏目是默认栏目，将其他栏目设置为默认栏目
        if(artistSectionTemp.getIsDefault().equals(1)){
            LambdaQueryWrapper<ArtistSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if(artistSectionTemp.getArtistId()!=null){
                lambdaQueryWrapper.eq(ArtistSectionTemp::getArtistId, artistSectionTemp.getArtistId());
            }
            List<ArtistSectionTemp> list = this.baseMapper.selectList(lambdaQueryWrapper);
            if(list.get(0).getIsDefault().equals(1)){
                this.baseMapper.updateById(list.get(0));
            }
        }
        // 删除内容板块
        if(artistSectionTemp.getSectionId()!=null&&!artistSectionTemp.getSectionId().equals("")){
            LambdaQueryWrapper<ArtistContentSection> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ArtistContentSection::getSectionId, artistSectionTemp.getSectionId());
            artistContentSectionMapper.delete(lambdaQueryWrapper);
        }
    }

    @Override
    public void saveBatchData(List<ArtistSectionTemp> artistSectionTempList) {
        for (int i = 0; i < artistSectionTempList.size(); i++) {
            LambdaQueryWrapper<ArtistSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ArtistSectionTemp::getSectionId, artistSectionTempList.get(i).getSectionId());
            this.baseMapper.update( artistSectionTempList.get(i),lambdaQueryWrapper);
        }
    }

    @Override
    public List<ArtistSectionTemp> getList(ArtistSectionTemp artistSectionTemp) {
        LambdaQueryWrapper<ArtistSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(artistSectionTemp.getArtistId()!=null){
            lambdaQueryWrapper.eq(ArtistSectionTemp::getArtistId, artistSectionTemp.getArtistId());
        }
        lambdaQueryWrapper.orderByAsc(ArtistSectionTemp::getSort);
        List<ArtistSectionTemp> list = this.baseMapper.selectList(lambdaQueryWrapper);
        return list;
    }
}
